<?php
  class editprofile extends admin{
    function make_extra($buff,&$buff){

	if(!isset($_GET['id'])){
	  $uid = (int)$_SESSION['uid'];
	} else {
	  $uid = (int)$_GET['id'];
	}

	$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."users_profiles WHERE uid = $uid ORDER BY profileid DESC LIMIT 1");
	$this->profile_row = $this->fetch_array($query);

	if($this->profile_row['sex'] == 1){
	  $this->male_class = 'save';
	  $this->female_class = 'publish';
	} else {
	  $this->male_class = 'publish';
	  $this->female_class = 'save';
	}
	$this->apploadlib(KLEO_LIB_DIR."forms.lib.php", "kleoforms", $forms);
	$this->tpl_set(array(
		"MALE_CLASS" => $this->male_class,
		"FEMALE_CLASS" => $this->female_class,
		"BIOS" => $this->profile_row['bios'],
		"BOOKS" => $this->profile_row['books'],
		"MUSIC" => $this->profile_row['music'],
		"MOVIES" => $this->profile_row['movies'],
		"SPORTS" => $this->profile_row['sports'],
		"TWITTER" => $this->profile_row['twitter'],
		"DIGG" => $this->profile_row['digg'],
		"FLICKR" => $this->profile_row['flickr'],
		"TOWN" => $this->profile_row['town'],
		"LASTFM" => $this->profile_row['lastfm'],
		"FACEBOOK" => $this->profile_row['facebook'],
		"WEBSITE" => $this->profile_row['web'],
		"YAHOO" => $this->profile_row['yahoo'],
		"MSN" => $this->profile_row['msn'],
		"GMAIL" => $this->profile_row['gmail'],
		"SEX" => $this->profile_row['sex'],
		"JOB" => $this->profile_row['job'],
		"NICENAME" => $this->profile_row['nicename'],
		"COUNTRY" => $forms->Get_Countries_Select($this->profile_row['country'])
	), false, $buff, $buff);
	
	if(isset($_POST['edit_profile'])){
		$_SESSION['dashboard_status'] = array(0,L_CHANGED_SUCCESFULLY);
		$nicename = $_POST['nicename'];
		$msn = $_POST['msn'];
		$yahoo = $_POST['yahoo'];
		$gmail = $_POST['gmail'];
		$web = $_POST['web'];
		$music = $_POST['music'];
		$movies = $_POST['movies'];
		$books = $_POST['books'];
		$sports = $_POST['sports'];
		$bios = $_POST['bios'];
		$sex = $_POST['sex'];
		$age = $_POST['age'];
		$job = $_POST['job'];
		$country = $_POST['country'];
		$town = $_POST['town'];
		$avatar = $_POST['avatar'];
		$delicious = $_POST['delicious'];
		$twitter = $_POST['twitter'];
		$digg = $_POST['digg'];
		$flickr = $_POST['flickr'];
		$lastfm = $_POST['lastfm'];
		$facebook = $_POST['facebook'];
		$avatar = $_POST['avatar'];

		if(!is_dir(KLEO_MEDIA_DIR.'avatars/'.$this->get_username($uid))){
			mkdir(KLEO_MEDIA_DIR.'avatars/'.$this->get_username($uid));
		}
		if($_FILES['avatar']['name'] !== null){
		  $target = KLEO_MEDIA_DIR.'avatars/'.$this->get_username($uid).'/'.basename($_FILES['avatar']['name']);

		  move_uploaded_file($_FILES['avatar']['tmp_name'], $target);
		  @chmod($target, 0755);

		  $useravatar = $this->get_username($uid).'/'.$_FILES['avatar']['name'];
		  $ouseravatar = $this->get_username($uid).'/thumb-'.$_FILES['avatar']['name'];
		}

		$check = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."users_profiles WHERE uid = $uid");
		if($this->num_rows($check) == 0){
		  $this->query("INSERT INTO `".KLEO_TABLE_PREFIX."users_profiles` (
			  `profileid` ,
			  `uid` ,
			  `nicename` ,
			  `msn` ,
			  `yahoo` ,
			  `gmail` ,
			  `web` ,
			  `music` ,
			  `movies` ,
			  `sports` ,
			  `bios` ,
			  `age` ,
			  `sex` ,
			  `job` ,
			  `country` ,
			  `town` ,
			  `delicious` ,
			  `twitter` ,
			  `digg` ,
			  `flickr` ,
			  `lastfm` ,
			  `facebook`
		  ) VALUES (
			  NULL , '$uid', '$nicename', '$msn', '$yahoo', '$gmail', '$web', '$music', '$movies', '$sports', '$bios', '$age', '$sex', '$job', '$country', '$town', '$delicious', '$twitter', '$digg', '$flickr', '$lastfm', '$facebook'
		  );");
		} else {
		  $this->query("UPDATE `".KLEO_TABLE_PREFIX."users_profiles` SET 
			`nicename` = '$nicename',
			`msn` = '$msn',
			`yahoo` = '$yahoo',
			`gmail` = '$gmail',
			`web` = '$web',
			`music` = '$music',
			`movies` = '$movies',
			`books` = '$books',
			`sports` = '$sports',
			`bios` = '$bios',
			`age` = '$age',
			`sex` = '$sex',
			`job` = '$job',
			`country` = '$country',
			`town` = '$town',
			`delicious` = '$delicious',
			`twitter` = '$twitter',
			`digg` = '$digg',
			`flickr` = '$flickr',
			`lastfm` = '$lastfm',
			`facebook` = '$facebook'
			WHERE `uid` = ".$uid." LIMIT 1 ;
		  ");
		  if(!empty($_POST['pass']) && $_POST['pass'] == $_POST['pass2']){
		  $pass = md5(sha1($_POST['pass']));
		  $this->query("UPDATE `".KLEO_TABLE_PREFIX."users` SET 
			`pass` = '$pass'
			WHERE `uid` = ".$uid." LIMIT 1 ;  ");			
		  }
		}

		if(!empty($_FILES['avatar']['name'])){
		  $this->query("UPDATE `".KLEO_TABLE_PREFIX."users_profiles` SET 
			`avatar` = '$ouseravatar'
			WHERE `profileid` =".$uid." LIMIT 1 ;");
		}

		require_once KLEO_LIB_DIR.'gd.lib.php';
		$gd = new kleogd();
		$gd->thumblr_static(KLEO_MEDIA_DIR.'avatars/'.$useravatar,64,64,KLEO_MEDIA_DIR.'avatars/'.$ouseravatar);
		$gd->thumblr_static(KLEO_MEDIA_DIR.'avatars/'.$useravatar,24,24,KLEO_MEDIA_DIR.'avatars/tiny-'.$ouseravatar);

		$this->dashboard_status = L_CHANGED_SUCCESFULLY;
	}
    }
  }
?>